new Vue({
	el: '#app',
	data: {
		uid: '',
		api_token: '',
		hobbyId: '',
		hobbyList: []
	},
	created: function() {
		console.log(" end");
		this.uid = $api.getStorage('uid');
		this.api_token = $api.getStorage('api_token');
		this.init();
	},
	methods: {
		init: function() {
			var self = this;
			if(self.uid) {
				apiready = function() {
					self.hobby();
				}
			} else {
				apiready = function() {
					self.layout();
				}
			}
		},
		//选择爱好
		choose: function(id) {
			var self = this;
			self.hobbyId = id;
			//del hobby
			api.confirm({
				title: '是否删除该爱好',
				buttons: ['确定', '取消']
			}, function(ret, err) {
				var index = ret.buttonIndex;
				console.log(index);
				if(index == 1) {
					self.delHobby(id);
				}
			});
		},
		delHobby: function(id) {
			var self = this;
			var data = {
				id: id,
				uid: self.uid,
				token: self.api_token
			};
			$.post(base + config.study_remove_hobby, data, function(msg) {
				console.log(msg);
				if(msg.error == 0) {
					api.toast({
						msg: "删除成功",
						duration: 500,
						location: 'middle'
					});
					self.hobby();
				} else {
					console.log(msg.error_msg);
					api.toast({
						msg: msg.error_msg,
						duration: 2000,
						location: 'middle'
					});
				}
			}, 'json');
		},
		//获取我的爱好列表
		hobby: function() {
			var self = this;
			var data = {
				uid: self.uid,
				token: self.api_token
			};
			$.post(base + config.study_my_hobby, data, function(msg) {
				console.log(msg);
				if(msg.error == 0) {
					self.hobbyList = msg.data;
				} else {
					console.log(msg.error_msg);
					api.toast({
						msg: msg.error_msg,
						duration: 2000,
						location: 'middle'
					});
				}
			}, 'json');
		},
		//添加爱好
		add: function() {
			var self = this;
			api.prompt({
				title: "添加我的爱好",
				buttons: ['确定', '取消']
			}, function(ret, err) {
				var index = ret.buttonIndex;
				var text = ret.text;
				console.log(text);
				console.log(index);
				if(index == 1) {
					self.addHobby(text);
				}
			});
		},
		addHobby: function(t) {
			var self = this;
			var data = {
				hobby: t,
				uid: self.uid,
				token: self.api_token
			};
			$.post(base + config.study_add_hobby, data, function(msg) {
				console.log(msg);
				if(msg.error == 0) {
					api.toast({
						msg: "添加成功",
						duration: 500,
						location: 'middle'
					});
					self.hobby();
				} else {
					console.log(msg.error_msg);
					api.toast({
						msg: msg.error_msg,
						duration: 2000,
						location: 'middle'
					});
				}
			}, 'json');
		},
		//跳转到全部课程列表页
		jumpAllClass: function() {
			if(this.uid) {
				api.openWin({
					name: 'allClass',
					url: 'allClass.html',
					vScrollBarEnabled: false,
					//					reload: true
				});
			} else {
				this.layout();
			}
		},
		//跳转到课程表
		jumpClass: function() {
			if(this.uid) {
				api.openWin({
					name: 'calendar',
					url: 'calendar.html',
					vScrollBarEnabled: false,
					//					reload: true
				});
			} else {
				this.layout();
			}
		},
		//跳转到我的课件
		jumpmyCourseware: function() {
			if(this.uid) {
				api.openWin({
					name: 'myCourseware',
					url: 'myCourseware.html',
					vScrollBarEnabled: false,
					//					reload: true
				});
			} else {
				this.layout();
			}
		},
		//跳转到学习经历
		jumpStudyExperience: function() {
			if(this.uid) {
				api.openWin({
					name: 'studyExperience',
					url: 'studyExperience.html',
					vScrollBarEnabled: false,
					//					reload: true
				});
			} else {
				this.layout();
			}
		},
		//跳转到录播课
		jumpTaped: function() {
			if(this.uid) {
				api.openWin({
					name: 'tapedLessons',
					url: 'tapedLessons.html',
					vScrollBarEnabled: false,
					//					reload: true
				});
			} else {
				this.layout();
			}
		},
		//登出
		layout: function() {
			api.openWin({
				name: 'login',
				url: '../../html/login/login.html',
				vScrollBarEnabled: false,
				reload: true,
				pageParam: {
					index: 3
				}
			});
		}
	}
})